package com.stone.common.util;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 邮件发送客户端工具类
 * @author Stone P
 *
 */
public class MailClient {

	static final Logger logger = LoggerFactory.getLogger(MailClient.class);

	//电子邮件主机信息
	private String host = "smtp.163.com";
	//源地址(如：)
	private String fromAddr = "breakingfast@163.com";
	//地址栏显示的名字(如：BreakingFast<breakingfast@163.com>)
	private String fromName = "BreakingFast";
	//邮件用户名
	private String username = "breakingfast";
	//用户密码
	private String password = "08082013.com";
	//邮件标题
	private String suject = "BreakingFast注册验证";
	//附加文件定义
	private String attachPath = null;
	private String attachName = null;
	//邮件内容
	private String strBufferCon = "";
	
	public MailClient() {
		super();
	}
	
	public MailClient(String host, String fromAddr, String username,
			String password, String suject, String attachPath,
			String attachName, String strBufferCon) {
		super();
		this.host = host;
		this.fromAddr = fromAddr;
		this.username = username;
		this.password = password;
		this.suject = suject;
		this.attachPath = attachPath;
		this.attachName = attachName;
		this.strBufferCon = strBufferCon;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getFromAddr() {
		return fromAddr;
	}

	public void setFromAddr(String fromAddr) {
		this.fromAddr = fromAddr;
	}

	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSuject() {
		return suject;
	}

	public void setSuject(String suject) {
		this.suject = suject;
	}

	public String getAttachPath() {
		return attachPath;
	}

	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}

	public String getAttachName() {
		return attachName;
	}

	public void setAttachName(String attachName) {
		this.attachName = attachName;
	}

	public String getStrBufferCon() {
		return strBufferCon;
	}

	public void setStrBufferCon(String strBufferCon) {
		this.strBufferCon = strBufferCon;
	}

	/**
	 * 发送Mail
	 * @param address		邮件地址
	 * @param checkCode		验证码
	 * @throws Exception
	 */
	public boolean sendMail(String address, String checkCode) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.host", this.host);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getDefaultInstance(props);
		session.setDebug(true);
		//会话对象的消息
		MimeMessage message = new MimeMessage(session);
		//设置的源地址
		String nick = "";
		try {  
            nick = MimeUtility.encodeText(fromName);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
		message.setFrom(new InternetAddress(nick + " <" + this.fromAddr + ">"));
		//设置收件人地址
		message.addRecipients(Message.RecipientType.TO, address);
		//抄送
//		message.addRecipients(Message.RecipientType.CC, "");
		//设置标题
		message.setSubject(this.suject);
		//设置邮件内容
		Multipart multipart = new MimeMultipart();
		BodyPart contentPart = new MimeBodyPart();
		contentPart.setText(this.strBufferCon + checkCode);
		//附件设置
		multipart.addBodyPart(contentPart);
		if (StringUtils.isNotBlank(this.attachPath) && StringUtils.isNotBlank(this.attachName)) {
			BodyPart attachmentPart = new MimeBodyPart();
			DataSource source = new FileDataSource(this.attachPath);
			attachmentPart.setDataHandler(new DataHandler(source));
			multipart.addBodyPart(attachmentPart);
		}
		message.setContent(multipart);
		message.saveChanges();
		Transport transport = session.getTransport("smtp");

		transport.connect(host, this.username, this.password);
		boolean sendStatus = true;
		try{
			transport.sendMessage(message, message.getAllRecipients());
		} catch (Exception e) {
			sendStatus = false;
		} finally {
			transport.close();
		}
		return sendStatus;
	}
}